package com.sk.bilibili.view.fragment.main;

import android.view.MenuItem;
import android.widget.PopupMenu;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.main.later.LaterAdapter;
import com.sk.bilibili.bean.LookLater;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.databinding.FragmentLaterBinding;
import com.sk.bilibili.databinding.FragmentSaveBinding;
import com.sk.bilibili.event.MainMenuEvent;
import com.sk.bilibili.model.fragment.main.LaterModel;
import com.sk.bilibili.model.fragment.main.SaveModel;
import com.sk.bilibili.utils.PopupMenuUtils;
import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;
import com.sk.bilibili.view.diy.bottomDialog.DeleteBottomDialog;
import com.sk.bilibili.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class LaterFragment extends BaseFragment<FragmentLaterBinding, LaterModel> implements PopupMenu.OnMenuItemClickListener, SwipeRefreshLayout.OnRefreshListener, OnBottomCallBack {

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
        deleteBottomDialog=new DeleteBottomDialog();
        onRefresh();
    }

    private LaterAdapter laterAdapter;
    private DeleteBottomDialog deleteBottomDialog;
    private PopupMenu menu;

    private void initView() {
        bind.srlShow.setColorSchemeResources(R.color.pink);
        bind.rvShow.setLayoutManager(new LinearLayoutManager(getContext()));
        laterAdapter = new LaterAdapter(getContext(), this);
        bind.rvShow.setAdapter(laterAdapter);
        menu = PopupMenuUtils.createPopupMenu(getContext(),bind.ivMore ,R.menu.later_menu);
    }

    private void bindEvent() {
        bind.ivMenu.setOnClickListener(v -> EventBus.getDefault().post(MainMenuEvent.closeEvent()));
        menu.setOnMenuItemClickListener(this);
        bind.ivMore.setOnClickListener(v->menu.show());
        bind.srlShow.setOnRefreshListener(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
    }

    public void onVideoSetClick(Integer id) {
        VideoSetActivity.start(getContext(),id);
    }

    public void onMemberClick(Integer id) {
        MemberActivity.start(getContext(),id);
    }

    public void onMoreClick(Integer id) {
        deleteBottomDialog.show(getChildFragmentManager(),id);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.delete:
                model.removeOver();
                break;
        }
        return true;
    }

    @Override
    public void onRefresh() {
        model.refresh();
    }

    @Override
    public void onBottom() {
        model.loadData();
    }

    public void loadDataSuccess(List<LookLater> records) {
        if(records.isEmpty()){
            toast("没有更多数据了");
        }else {
            laterAdapter.addData(records);
        }
    }

    public void refreshSuccess(List<LookLater> records) {
        laterAdapter.setData(records);
        bind.srlShow.setRefreshing(false);
    }

    public void setNum(Integer total) {
        bind.tvTitle.setText(String.format("稍后再看(%d)",total));
    }

}